package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;

/**
 * 埋点监听
 */
public class DataAnalyticsListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        if(syncType == eUnionSyncType.CreateUnion.getValue()){
            CrossUnionMember member = memberList.get(0);
            if(member != null && member.getServerId() == GameServer.getInstance().getServerId()) {
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                if (player != null) {
                    DataAnalyticsMgr.trackUnionCreateGuild(player, unionInfo);
                }
            }
        }


        if(syncType == eUnionSyncType.DeleteUnion.getValue()){
            for (CrossUnionMember member : memberList) {
                if(member.getServerId() == GameServer.getInstance().getServerId() && playerId == member.getUserId()){
                    GamePlayer player = GamePlayerMgr.getOnlinePlayer(playerId);
                    if(player != null) {
                        DataAnalyticsMgr.trackUnionBreakGuild(player, unionInfo);
                    }
                }
            }
        }

        if(syncType == eUnionSyncType.JoinUnion.getValue()){
            CrossUnionMember member = memberList.get(0);
            if(member != null && member.getServerId() == GameServer.getInstance().getServerId()) {
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                if (player != null) {
                    DataAnalyticsMgr.trackUnionJoinGuild(player.getUserInfo(), unionInfo, eUnionPosition.Member.getValue());
                }
            }
        }
        if(syncType == eUnionSyncType.BeKick.getValue() || syncType == eUnionSyncType.QuitUnion.getValue()){
            CrossUnionMember member = memberList.get(0);
            if(member != null && member.getServerId() == GameServer.getInstance().getServerId()) {
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                if (player != null) {
                    DataAnalyticsMgr.trackUnionLeaveGuild(player.getUserId(), unionInfo, member.getPosition());
                }
            }
        }
    }
}
